package com.example.lxh.testinfo;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import junit.framework.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.ListViewAdapter;

public class MainActivity extends AppCompatActivity {
    private ListView m_lv;
    private List<Map<String, Object>> mData;
    public ListViewAdapter adapter;
    private PopupWindow popupWindow;
    int popupWindHeigh = 0;
    int popupWindowWidth=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        m_lv = findViewById(R.id.lv_car);
        mData = getData();
        adapter = new ListViewAdapter(MainActivity.this, mData);
        m_lv.setAdapter(adapter);
        adapter.setOnItemPayClickListener(new ListViewAdapter.OnItemPayListener() {
            @SuppressLint("ResourceType")
            @Override
            public void OnPayClick(int i) {
                String plate = (String) mData.get(i).get("plate");
                if (popupWindow != null) {
                    popupWindow.dismiss();
                    return;
                } else {
                    initpopupWindow();
                    popupWindow.showAsDropDown(findViewById(R.layout.popupwindowlayout),Gravity.CENTER, 0, 0);
                }
            }
        });
    }

    private void initpopupWindow() {
        int screenWidth = this.getWindowManager().getDefaultDisplay().getWidth();
        int screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
        //获取自定义布局文件popupwindow.xml
        View popupWindow_view = getLayoutInflater()
                .inflate(R.layout.popupwindowlayout, null, false);
        popupWindow_view.setBackgroundResource(R.drawable.back);
        //创建popupWindow实例
        popupWindow = new PopupWindow(popupWindow_view, screenWidth - 40, screenHeight / 3 - 20, true);

        popupWindHeigh=popupWindow.getHeight();
        popupWindowWidth=popupWindow.getWidth();
    }

    public List<Map<String, Object>> getData() {
        List<Map<String, Object>> data = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", "秦二世" + i);
            map.put("plate", "秦 NB123456" + i);
            map.put("money", "100元" + i);
            map.put("number", "1" + i);
            data.add(map);
        }
        return data;
    }
}
